home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / sim.lha / sim / builtin / arith.c < prev    next >
C/C++ Source or Header  |  1990-04-12  |  4KB  |  109 lines

  1. /************************************************************************
  2. *                                                                       *
  3. * The SB-Prolog System                                                  *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987      *
  5. *                                                                       *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* arith.c */
  25.  
  26. /*  some builtins for arithmetic functions  */
  27.  
  28. #include "builtin.h"
  29.  
  30. extern double floatval();
  31. extern LONG   makefloat();
  32.  
  33. b_FLOATC()    /* F, M, E, S */
  34. {
  35.    /* reg1 (F) is either a variable or a number (in WAM format);
  36.     * reg2 (M) is either a number (in WAM format) or a variable;
  37.     * reg3 (E) is either an integer or a variable.  The intended
  38.     * interpretation is that M and E represent the mantissa and exponent,
  39.     * respectively, of the floating point number F.  Either F, or 
  40.     * both M and E, are assumed to be bound (the selector reg4 (S)
  41.     * tells us which case it is).  No checking for this is done here.
  42.     */
  43.  
  44.    register LONG     op1, op2, op3, op4;
  45.    register LONG_PTR top;
  46.    double            fval; 
  47.    int               exp0;
  48.  
  49.    op1 = reg[1];  DEREF(op1);
  50.    op2 = reg[2];  DEREF(op2);
  51.    op3 = reg[3];  DEREF(op3);
  52.    op4 = reg[4];  DEREF(op4);
  53.  
  54.    switch (INTVAL(op4)) {
  55.       case 0: fval = frexp(NUMVAL(op1), &exp0);
  56.               FOLLOW(op2) = makefloat(fval);
  57.           PUSHTRAIL(op2);
  58.               FOLLOW(op3) = MAKEINT(exp0);
  59.           PUSHTRAIL(op3);
  60.               break;
  61.       case 1: fval = ldexp((double)NUMVAL(op2), (int)INTVAL(op3));
  62.               FOLLOW(op1) = makefloat(fval);
  63.           PUSHTRAIL(op1);
  64.               break;
  65.       case 3: fval = ldexp((double)NUMVAL(op2), (int)INTVAL(op3));
  66.               if (floatval(op1) != fval) {FAIL0;}
  67.               break;
  68.    }
  69. }
  70.  
  71. b_ARITH()
  72. {
  73.    register LONG     op1, op2, op3;
  74.    register LONG_PTR top;
  75.    double            y;
  76.  
  77.    op1 = reg[1]; 
  78.    op2 = reg[2];
  79.    op3 = reg[3];  DEREF(op3);
  80.  
  81.    switch (INTVAL(op3)) {
  82.       case 0: DEREF(op2);
  83.               if (!unify(op1, makefloat(log((double)NUMVAL(op2)))))
  84.                  {FAIL0;}
  85.               break;
  86.       case 1: DEREF(op1);
  87.               if (!unify(makefloat(exp((double)NUMVAL(op1))), op2))
  88.                  {FAIL0;}
  89.               break;
  90.       case 2: DEREF(op2);
  91.               if (!unify(op1, makefloat(sqrt((double)NUMVAL(op2)))))
  92.                  {FAIL0;}
  93.               break;
  94.       case 3: DEREF(op1);
  95.               y = (double)NUMVAL(op1);
  96.               if (!unify(makefloat(y * y), op2)) 
  97.                  {FAIL0;}
  98.               break;
  99.       case 4: DEREF(op1);
  100.               if (!unify(makefloat(sin((double)NUMVAL(op1))), op2))
  101.                  {FAIL0;}
  102.               break;
  103.       case 5: DEREF(op2);
  104.               if (!unify(op1, makefloat(asin((double)NUMVAL(op2)))))
  105.                  {FAIL0;}
  106.               break;
  107.    }
  108. }
  109.